//
//  RootServvice.m
//  HTHappySale
//
//  Created by 创维海通 on 2017/9/14.
//  Copyright © 2017年 zxyywy. All rights reserved.
//

#import "RootServvice.h"
#import "Constants.h"
#import "VariableRange.h"
#import "WXAuth2ViewController.h"
#import "LoginViewController.h"
#import "MainViewController.h"
#import "WQUserDataManager.h"
#import "RegisterInfoViewController.h"

#import "BaseNavigationController.h"
#import "HTAssembleKey.h"

#import "HTTPPromise+Login.h"
@implementation RootServvice

ML_EXPORT_SERVICE(RootService);

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    application.delegate.window = window;
    
    //获取IDFV
    [self getIDFV];

//    HTCFUDFRemove(kAuthorizationKey);
//    HTCFUDFRemove(kMobileKey);
    authorizationCode = HTCFUDFGetValue(kAuthorizationKey);
    
    [[HTAssembleKey sharedTool] assembleKey];
    
    NSLog(@"%@",authorizationCode);
    NSLog(@"%@",mobileStr);
    NSLog(@"roleType:%d",roleType);
    //微信注册
    [self RegisterWX];
    if(HTCFStringIsNull(authorizationCode))
    {
        
        WXAuth2ViewController *WXAuthVC = [[WXAuth2ViewController alloc] init];
        
        BaseNavigationController *WXAuthNac = [[BaseNavigationController alloc] initWithRootViewController:WXAuthVC];
        window.rootViewController = WXAuthNac;
    }else if(HTCFStringIsNull(mobileStr)){
        //判断是否已经注册
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        
        BaseNavigationController *loginNac = [[BaseNavigationController alloc] initWithRootViewController:loginVC];
        window.rootViewController = loginNac;
    }else if (roleType ==  -1)
    {
        RegisterInfoViewController *registerInfo = [[RegisterInfoViewController alloc] init];
        BaseNavigationController *registerInfoNac = [[BaseNavigationController alloc] initWithRootViewController:registerInfo];
        window.rootViewController = registerInfoNac;
    }else{
        MainViewController *mainVC = [[MainViewController alloc] init];
        window.rootViewController = mainVC;
    }
    
    [window makeKeyAndVisible];

    return YES;
}

- (void)RegisterWX{
    //向微信注册
    [WXApi registerApp:WXKey enableMTA:YES];
    
    //向微信注册支持的文件类型
    UInt64 typeFlag = MMAPP_SUPPORT_TEXT | MMAPP_SUPPORT_PICTURE | MMAPP_SUPPORT_LOCATION | MMAPP_SUPPORT_VIDEO |MMAPP_SUPPORT_AUDIO | MMAPP_SUPPORT_WEBPAGE | MMAPP_SUPPORT_DOC | MMAPP_SUPPORT_DOCX | MMAPP_SUPPORT_PPT | MMAPP_SUPPORT_PPTX | MMAPP_SUPPORT_XLS | MMAPP_SUPPORT_XLSX | MMAPP_SUPPORT_PDF;
    
    [WXApi registerAppSupportContentFlag:typeFlag];
}

- (void)getIDFV
{
    if ([WQUserDataManager readUniqueIdentifier] == nil) {
        
        IDFV = HTCFSystemIDFV;
        [WQUserDataManager saveUniqueIdentifier:IDFV];
    }else
    {
        IDFV = [WQUserDataManager readUniqueIdentifier];
    }
}

#define isVersion HTCFSystemIsOver10 ? (YES) :(NO)
//#ifdef isVersion
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
    
    return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}
//#else
//handleOpenURL(ios10已弃用)
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    
    return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}

//openURL(iOS10已弃用)
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation{
    
    return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}
//#endif

@end
